package cn.tp.ksoft.civil_driver.request;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.civil_driver.utils.TimestampTypeAdapter;
import cn.tp.ksoft.library_civil.utils.StringUtils;

public class RequestParams implements Serializable {

	private static final long serialVersionUID = 1L;

	private String project_name; // 服务项目名字

	private String model_name; //模块名
	private String servlet_name; // 服务器servlet名字

	private String server_ip; // 服务器 服务地址
	private LinkedHashMap<String, Object> params = null;// 参数
	private boolean isShowProgress = false; // 是否显示进度条
	private Context ctx;
	
	
	public String getModel_name() {
		return model_name;
	}

	public void setModel_name(String model_name) {
		this.model_name = model_name;
	}

	public String getProject_name() {
		return project_name;
	}

	public void setProject_name(String project_name) {
		this.project_name = project_name;
	}

	public String getServlet_name() {
		return servlet_name;
	}

	public void setServlet_name(String servlet_name) {
		this.servlet_name = servlet_name;
	}

	public String getServer_ip() {
		return server_ip;
	}

	public void setServer_ip(String server_ip) {
		this.server_ip = server_ip;
	}

	public LinkedHashMap<String, Object> getParams() {
		return params;
	}

	public boolean isShowProgress() {
		return isShowProgress;
	}
	public Context getContext() {
		return ctx;
	}
	public void setContext(Context ctx) {
		this.ctx = ctx;
	}
	public void setShowProgress(boolean isShowProgress) {
		this.isShowProgress = isShowProgress;
	}

	/**
	 * 返回URL
	 * 
	 * @return
	 */
	public String getURL() {
		return "http://" + server_ip + "/" + project_name + "/" + model_name + "/"+ servlet_name;
	}

	public void addParams(String name, Object value) {
		if (null == this.params) {
			this.params = new LinkedHashMap<String, Object>();
		}
		this.params.put(name, value);
	}
	
	public void addFile(String name,File value){
		addParams(name,value);
	}
	
	/**
	 * 处理复杂实体或传输类，将字段拆分作为参数处理
	 * @param dto
	 */
	public void addDtoParams(Object dto){
		if (null == this.params) {
			this.params = new LinkedHashMap<String, Object>();
		}
		if(dto != null){
			splitFields(null, dto);
		}
	}
	
	/**
	 * 处理复杂实体或传输类
	 * TODO: 未处理多层嵌套复杂对象的情况
	 * @param name		参数名称
	 * @param value		复杂实体类对象
	 */
	public void addDtoParams(String name,Object value){
		if (null == this.params) {
			this.params = new LinkedHashMap<String, Object>();
		}
		if(value != null){
			splitFields(name, value);
		}
	}

	private void splitFields(String name, Object value) {
		Field[] fields = value.getClass().getDeclaredFields();
		for(Field field : fields){
			String fname =field.getName();
			field.setAccessible(true);
			try {
				Object fval=field.get(value);
				// 空的情况，不发送给后台
				if(fval != null){
					String tname = StringUtils.isEmpty(name)?"":name+".";
					String key = tname+fname;
					if(fval instanceof Date){
						// 日期需要格式化处理，前后台统一格式
						DateFormat format =  new SimpleDateFormat(Constants.FORMAT_DATE_COMMON_PATTERN);
						this.params.put(key, format.format((Date)fval));
					}else if(fval instanceof List){
						List list = (List) fval;
						for (int i = 0; i < list.size(); i++) {
							splitFields(key+"["+i+"]",list.get(i));
						}
					}else{
						this.params.put(key, fval);
					}
				}
			} catch (Exception e) {
				
			} 
		}
	}

	public void addListDtoParams(String key, List list) {
		if (null == this.params) {
			this.params = new LinkedHashMap<String, Object>();
		}
		for (int i = 0; i < list.size(); i++) {
			splitFields(key+"["+i+"]",list.get(i));
		}
	}

	public void addJsonParams(String string, Object obj) {
		if (null == this.params) {
			this.params = new LinkedHashMap<String, Object>();
		}
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Date.class, new TimestampTypeAdapter());
		Gson gson = gsonBuilder.create();
		this.params.put(string, gson.toJson(obj));
	}
}
